import {Toast} from "vant";

export function checkShake(callback) {
    if (window.DeviceMotionEvent) {
        let speed = 20;    // 用来判定的加速度阈值，太大了则很难触发
        let x, y, z, lastX, lastY, lastZ;
        x = y = z = lastX = lastY = lastZ = 0;
        window.addEventListener('devicemotion', function (event) {
            const acceleration: any = event.accelerationIncludingGravity;
            x = acceleration.x;
            y = acceleration.y;
            if (Math.abs(x - lastX) > speed || Math.abs(y - lastY) > speed) {
                callback && callback();
            }
            lastX = x;
            lastY = y;
        });
    }
}

export function freeze(obj) {
    return Object.freeze(obj)
}

export function thenSet(obj: object, key: string, targetKey = "records") {
    return (data) => obj[key] = targetKey ? data[targetKey] : data;
}

export function failToast(message) {
    return Toast.fail({message})
}

export function objHasNull(obj) {
    return Object.values(obj).some((v) => v == null)
}